require("apm_core")
require("extension")

-- debug开关，用来控制是否输出日志，测试时可以设为true打开，但长期运行时建议设为false关闭以提高性能
local G_DEBUG = true

if G_DEBUG == false then
    function logDebug(fmt,...) end
    function logInfo(fmt,...) end
    function logWarn(fmt,...) end
    function logError(fmt,...) end
    function logFatal(fmt,...) end
end

function post_analysis()
    local ApmData = require("ApmData")

    -- 配置上报消息类型，1表示响应，即仅处理消息类型为响应的数据
    if ApmData.getMsgType() ~= 1 then return end

    local s_type_name

    -- 校验上报消息类型，不包括问号及后面的参数
    local url = ApmData.getField("transaction.title", 1)
    if string.endswith(url, "/portal/service/") then
        s_type_name = "运维门户-综合服务台"
    elseif string.endswith(url, "/itsm/itil/mydeskdop.jsp") then
        s_type_name = "运维门户-我的工作台"
    elseif url == "http://10.99.102.75:8890" then
        s_type_name = "运维管理系统"
    elseif string.endswith(url, "/itsm/information/leftMenu.jsp") then
        s_type_name = "运维门户-信息互联互通"
    elseif string.endswith(url, "/itsm/ais/aisSearch!tak.action") then
        s_type_name = "运维门户-全文检索"
    elseif string.endswith(url, "/itsm/monitor/arbiter.jsp") then
        s_type_name = "监控管理-告警台"
    elseif string.endswith(url, "/live/carrier/topoMenu.action") then
        s_type_name = "监控管理-拓扑展现"
    elseif string.endswith(url, "/itsm/pcserver/pcServerAction.action") then
        s_type_name = "监控管理-PC终端管理"
    elseif string.endswith(url, "/itsm/operation_control/") then
        s_type_name = "监控管理-集中操作"
    elseif string.endswith(url, "/itsm/specialMonitor/specialMonitor.action") then
        s_type_name = "监控管理-视频监控"
    elseif url == "http://10.99.114.200/customsv7/" then
        -- 该IP地址不是COSS节点的IP
        s_type_name = "监控管理-动环监控"
    elseif string.endswith(url, "/3d/index.jsp") then
        s_type_name = "监控管理-3D机房"
    elseif string.endswith(url, "/itsm/kb/kb.action") then
        s_type_name = "运维流程-知识库"
    elseif string.endswith(url, "/cmdb/coredb/resMonitor.action") then
        s_type_name = "监控管理-性能监控"
    elseif string.endswith(url, "/itsm/commonManage/") then
        s_type_name = "运维流程-常规运行管理"
    elseif string.endswith(url, "/cmdb/coredb/coredb.action") then
        s_type_name = "配置管理-配置管理"
    elseif string.endswith(url, "/cid/") then
        s_type_name = "配置管理-自动发现"
    elseif string.endswith(url, "/cmdb/coredb/audit.jsp") then
        s_type_name = "配置管理-配置审核"
    elseif string.endswith(url, "/cmdb/machine/machine_info.jsp") then
        s_type_name = "机房管理-基础信息管理"
    elseif string.endswith(url, "/cmdb/machine/machine_device.jsp") then
        s_type_name = "机房管理-机房动环设施管理"
    elseif string.endswith(url, "/cmdb/machine/machine_reform.jsp") then
        s_type_name = "机房管理-机房改造管理"
    elseif string.endswith(url, "/cmdb/machine/machine_material.jsp") then
        s_type_name = "机房管理-规范资料管理"
    elseif string.endswith(url, "/webtopo/reports/birt/topolinereport/topoLineReportFrame.jsp") then
        s_type_name = "统计报表-终端连接报表"
    elseif url == "http://10.99.102.75/pubsite/" then
        s_type_name = "运维服务门户网站"
    end

    -- 提取完整的URL，包括问号后面的参数
    if s_type_name == nil then
        url = ApmData.getField("request.http.url", 1)
        if string.endswith(url, "/itsm/bizobject/biz.action?command=itil&bizModule.id=serviceReport&bizModule.name=服务申告") then
            s_type_name = "运维流程-基础流程管理"
        elseif string.endswith(url, "/live/model/viewer.jsp?viewId=ea2b6286-0945-4435-a9c3-8dea467c29ab") then
            s_type_name = "统计报表-度量分析"
        elseif string.endswith(url, "/itsm/report/renderReport.jsp?menuId=report_metric") then
            s_type_name = "统计报表-运维度量统计报表"
        elseif string.endswith(url, "/itsm/report/renderReport.jsp?menuId=report_performance") then
            s_type_name = "统计报表-性能统计报表"
        elseif string.endswith(url, "/itsm/report/renderReport.jsp?menuId=report_alarm") then
            s_type_name = "统计报表-告警统计报表"
        elseif string.endswith(url, "/itsm/report/renderReport.jsp?menuId=report_asset") then
            s_type_name = "统计报表-配置统计报表"
        elseif string.endswith(url, "/itsm/report/renderReport.jsp?menuId=report_service") then
            s_type_name = "统计报表-服务流程报表"
        elseif string.endswith(url, "/portal/ossite/role/ossite-kjs/release.psml?domainId=rootDomain&userId=") then
            s_type_name = "运维服务网站-发布专区"
        elseif string.endswith(url, "/portal/ossite/role/ossite-kjs/kb.psml?domainId=rootDomain&userId=") then
            s_type_name = "运维服务网站-知识库"
        end
    end

    if s_type_name ~= nil then
        ApmData.replaceField("transaction.type_code", s_type_name)
        ApmData.replaceField("transaction.type_name", s_type_name)
        -- 打印日志，会出现在/opt/apm/analyze/dist/logs/lua.log
        logInfo("type_name:[%s]", s_type_name)
    end

    return
end
--v1.0.0
